package com.lbd.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.lbd.gulimall.product.entity.ProductAttrValueEntity;
import com.lbd.gulimall.product.service.ProductAttrValueService;
import com.lbd.gulimall.product.vo.AttrResponseVO;
import com.lbd.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lbd.gulimall.product.service.AttrService;
import com.lbd.common.utils.PageUtils;
import com.lbd.common.utils.R;



/**
 * 商品属性
 *
 * @author linbd
 * @email 2236000738@qq.com
 * @date 2021-06-04 17:54:47
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;


    /**
     * 获取spu规格
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList  = productAttrValueService.listForSpu(spuId);
        return R.ok().put("data",productAttrValueEntityList);
    }

    @PostMapping("/update/{spuId}")
    public R updateAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        productAttrValueService.updateAttr(spuId,productAttrValueEntities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 基本列表查询  base/sale
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("type") String type){
        PageUtils page = attrService.queryAttrList(params,catelogId,type);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponseVO attrResponseVO = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResponseVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attrVO){
		attrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attrVO){
		attrService.updateAttr(attrVO);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
